<template>
  <div class="loginBox">
    <div class="loginFrom">
      <h3>叩丁严选后台管理</h3>
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        status-icon
        :rules="rules"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input
            :prefix-icon="UserFilled"
            placeholder="请输入用户名"
            v-model="ruleForm.username"
            type="text"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            :prefix-icon="Lock"
            placeholder="请输入密码"
            v-model="ruleForm.password"
            type="password"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)"
            >Submit</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { useRouter } from "vue-router";
import type { FormInstance, FormRules } from "element-plus";
import { UserFilled, Lock } from "@element-plus/icons-vue";
import * as http from "@/request/http";
import { ElMessage } from "element-plus";
import { useUserInfo } from "@/store/userInfo";

const ruleForm = reactive({
  username: "admin",
  password: "123456",
});

const userInfo = useUserInfo();

const router = useRouter();

const ruleFormRef = ref<FormInstance>();

const validatePass = (rule: any, value: any, callback: any) => {
  console.log("验证方法：", rule, value);
  let reg = /^[a-zA-Z0-9]{6}$/;
  if (value === "") {
    callback(new Error("请输入密码！"));
  } else if (!reg.test(value)) {
    callback(new Error("请输入正确的密码密码只能是数字和字符"));
  } else {
    callback();
  }
};

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      http.admin
        .userAdminLogin({
          username: ruleForm.username,
          password: ruleForm.password,
        })
        .then((res) => {
          if (res.code === 200) {
            ElMessage({
              message: "登录成功",
              type: "success",
            });
            userInfo.getToken(res.data.tokenHead + res.data.token);
            // http.admin.userInfo().then(res => {
            //     userInfo.upDateMenus(res.data.menus)
            router.push("/index");
            // })
          } else {
            ElMessage({
              message: "登录失败",
              type: "warning",
            });
          }
        });
    } else {
      ElMessage({
        message: "登录失败",
        type: "warning",
      });
      return false;
    }
  });
};

const rules = reactive<FormRules<typeof ruleForm>>({
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ validator: validatePass, trigger: "blur" }],
});
</script>

<style scoped lang="less">
.loginBox {
  width: 100%;
  height: 100%;
  background-color: #2d3a4b;
  position: relative;

  .loginFrom {
    width: 400px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);

    :deep(.el-input__wrapper) {
      background-color: transparent;
    }

    :deep(.el-button) {
      width: 100%;
    }

    h3 {
      text-align: center;
      margin-bottom: 20px;
      color: #fff;
    }
  }
}
</style>@/store/useStore